<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Bank extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('bank_mdl');
    }

    /**
     * 银行卡列表(用户)
     */
    public function index(){
        if($_SESSION['group'] != 3){
            show_error('没有权限！');
        }
        
        //查询银行卡
        $this->db->start_cache();
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->stop_cache();
        
        $this->load->library('pagination');
        $config['base_url'] = site_url('bank/index');
        $config['total_rows'] = $this->bank_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';
        
        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();
        
        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('id desc');
        $data['list'] = $this->bank_mdl->select();
        $this->db->flush_cache();
        $this->_view('index',$data);
    }

    /**
     * 新增银行卡
     */
    public function create(){
        if($_SESSION['group'] != 3){
            show_error('没有权限！');
        }

        if($this->input->is_ajax_request()){
            $post = $this->input->post();
            if(empty($post)){
                die('参数错误！');
            }

            if(empty($post['name'])){
                die('用户姓名不能为空');
            }
            if(!preg_match("/^[0-9]{10,20}$/",$post['number'])){
                die('请正确填写银行卡卡号');
            }
            if(empty($post['remarks'])){
                die('请正确填写开户行');
            }

            $data = array(
                'name' => $post['name'],
                'number' => intval($post['number']),
                'user_id' => $_SESSION['user']['id'],
                'remarks' => $post['remarks'],
                'create_date' => $_SERVER['REQUEST_TIME']
            );

            $res = $this->bank_mdl->create($data);
            echo $res === FALSE ? '添加失败！' : '1';
        }else{
            $this->_view('create');
        }
    }
    
    /**
     * 银行卡修改
     */
    public function update(){
    	if($_SESSION['group'] != 3){
    		show_error('没有权限！');
    	}
    	
    	if($this->input->is_ajax_request()){
    		$post = $this->input->post();
    		$bid = $this->input->post('bid');
    		
    		$this->db->where('id',$bid);
    		$this->db->where('user_id',$_SESSION['user']['id']);
    		$res = $this->bank_mdl->get_one();
    		if(empty($res)){
    			die('该数据不存在！');
    		}
    		
    		if(empty($post['name'])){
    			die('用户姓名不能为空');
    		}
    		if(!preg_match("/^[0-9]{10,20}$/",$post['number'])){
    			die('请正确填写银行卡卡号');
    		}
    		if(empty($post['remarks'])){
    			die('请正确填写开户行');
    		}
    		
    		$data = array(
                'name' => $post['name'],
                'number' => intval($post['number']),
                'remarks' => $post['remarks']
            );
    		
    		$r = $this->bank_mdl->update($data);
    		echo $r === true ? '1' : '修改数据失败！';
    		
    	}else{
    		$bid = $this->input->get('bid');
    		$this->db->where('id',$bid);
    		$this->db->where('user_id',$_SESSION['user']['id']);
    		$res = $this->bank_mdl->get_one();
    		
    		if(empty($res)){
    			show_error('该数据不存在！');
    		}
    		
    		$this->_view('update',array('res'=>$res));
    	}
    }
    
    /**
     * 银行卡删除
     */
    public function delete(){
    	if(!$this->input->is_ajax_request()){
    		show_error('请求方式错误！');
    	}
    	
    	$bid = $this->input->post('bid');
    	$this->db->where('id',$bid);
    	$this->db->where('user_id',$_SESSION['user']['id']);
    	$res = $this->bank_mdl->get_one();
    	
    	if(empty($res)){
    		die('数据不存在');
    	}
    	
    	$this->db->where('id',$res['id']);
    	$r = $this->bank_mdl->delete();
    	echo $r == true ? 1 : '删除失败！';
    }

}

/* End of file bank.php */
/* Location: ./application/controllers/bank.php */